<?php
Zend_Loader::loadClass('Zend_Validate');
Zend_Loader::loadClass('Lead_EDO_Datatype_Validator_Interface');

class Lead_EDO_Datatype_Validator
{
    
    
    static public function validate(Lead_EDO_Datatype_Field_Interface $field, $value)
    {
        $validatorClass = 'Lead_EDO_Datatype_Validator_' . ($field instanceof Lead_EDO_Datatype_Field ? 'Field' : 'Relation') . '_' . $field->getType();
        
        try {
            
            // If the value can be null and it is, no need to validate further
            if(!$field->isNotNull() AND $value === null) {
                return true;
            }
            
            // Otherwise let's proceed with validation.
            Zend_Loader::loadClass($validatorClass);
            $validator = call_user_func(array($validatorClass, 'get'), $field);
            $isValid = $validator->isValid($value);

            if(!$isValid) {
                print_r($validator);
            }
            
            
            $field->setValidationMessages($validator->getMessages());
            return $isValid;
        } catch(Zend_Exception $e) {
            // Catch nonexisting validator and return false.
            $field->setValidationMessages("Could not load validator for '{$field->getType()}' field.");            
            
            throw $e;
            
            return false;
        }
        
        
    }
    
    
    
    
}
